package com.yzt.loginmodule.regist;

import com.alibaba.fastjson.JSONObject;
import com.yzt.p_base.utils.SharedPreferenceUtil;
import com.yzt.zhuju.RXingUtils;
import com.yzt.zhuju.arouter.RouterMain;
import com.yzt.zhuju.base.BaseActivity;
import com.yzt.zhuju.network.AresSubscriber;
import com.yzt.zhuju.network.biz.BizLayer;
import com.yzt.zhuju.network.entity.user.UserLoginInfo;
import com.yzt.zhuju.widget.pickview.TimePickerViewManager;
import com.yzt.zhuju.widget.tuiview.TUITextView;
import com.yzt.loginmodule.R;
import com.yzt.p_base.utils.DateUtils;
import com.yzt.plugin_network.response.HttpResponse;

import java.util.Calendar;

/**
 * author : bob
 * date   : 2022/12/2
 * desc   : 注册选择生日
 */
public class RegisterChooseBirthdayActivity extends BaseActivity {

    private TUITextView tvChooseBirthday;

    private Calendar startDate;

    private TUITextView tvNext;

    private TUITextView tv_y;
    private TUITextView tv_m;
    private TUITextView tv_d;

    private String birthday;

    private int gender;

    private String nickname;

    private String avatar;

    @Override
    public int getLayoutId() {
        return R.layout.activity_choose_birthday;
    }

    @Override
    public void installViews() {
        gender = getIntent().getIntExtra("extra_gender", 0);
        nickname = getIntent().getStringExtra("extra_nickname");
        avatar = getIntent().getStringExtra("extra_header");
        tvChooseBirthday = findViewById(R.id.tv_choose_birthday);
        tvNext = findViewById(R.id.tv_next);
        tv_y = findViewById(R.id.tv_y);
        tv_m = findViewById(R.id.tv_m);
        tv_d = findViewById(R.id.tv_d);

        findViewById(R.id.iv_back).setOnClickListener(v -> finish());

        tvChooseBirthday.setOnClickListener(v -> getTime());
        tv_y.setOnClickListener(v -> getTime());
        tv_m.setOnClickListener(v -> getTime());
        tv_d.setOnClickListener(v -> getTime());

        tvNext.setOnClickListener(v -> {

            if (!tvNext.isSelected()) {
                return;
            }

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("nickName", nickname);
            jsonObject.put("gender", gender);
            jsonObject.put("avatar", avatar);
            jsonObject.put("birthday", birthday);

            BizLayer.getInstance().getAccountModule().registerEditUserInfo(jsonObject, new AresSubscriber<HttpResponse>() {
                @Override
                protected void onSuccess(HttpResponse response) {
                    UserLoginInfo mUserLoginInfo = RXingUtils.getLoginAccount();
                    mUserLoginInfo.setEvent(UserLoginInfo.EVENT_TYPE_LOGIN);
                    RXingUtils.saveLoginAccount(mUserLoginInfo);
                    RXingUtils.setGender(gender);
                    SharedPreferenceUtil.getInstance().putValue(RXingUtils.LOGIN_REGISTER, true);
                    RouterMain.MainActivity();
                    finish();
                }

                @Override
                protected void onError(int code, String info) {
                    super.onError(code, info);
                }
            });
        });
    }

    /**
     * 获取当前时间
     */
    private void getTime() {

        TimePickerViewManager mTimeManager = TimePickerViewManager.getInstance(this);
        startDate = Calendar.getInstance();
        int year = startDate.get(Calendar.YEAR);
        int minute = startDate.get(Calendar.MONTH);
        int dayOfMonth = startDate.get(Calendar.DAY_OF_MONTH);
        startDate.set(year - 99, minute, dayOfMonth);

        Calendar endDate = Calendar.getInstance();
        endDate.set(year - 18, minute, dayOfMonth);

        Calendar defaultDate = Calendar.getInstance();
        defaultDate.set(year - 25, minute, dayOfMonth);

        mTimeManager.setSelectCalendar(defaultDate);
        mTimeManager.initTimePickerView(mActivity, startDate, endDate, date -> {
            //保留选择后的时间
            startDate.setTime(date);
            birthday = DateUtils.getDateTime(date, "yyyy-MM-dd");
            tvChooseBirthday.setText(birthday);
            tvNext.setSelected(true);
            tv_y.setText(birthday.substring(0, 4));
            tv_m.setText(birthday.substring(5, 7));
            tv_d.setText(birthday.substring(8, 10));
        });
        mTimeManager.showTimePicmkerView();
    }
}
